package com.yjf.ljr.service;

import com.yjf.ljr.application.MyApplication;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.connection.IjConnect;
import com.yjf.ljr.dataclass.EasyDataClass;
import com.yjf.ljr.parser.RequestBuilder;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;

import android.app.IntentService;
import android.content.Intent;

public class AutoLoginService extends IntentService{
	
	public static boolean autoLogin = true;
	public static long DELAY = 20*60*1000;
	
	public AutoLoginService() {
		super("autologinservice");
	}

	@Override
	protected void onHandleIntent(Intent intent) {		
		keepConnecting();
	}

	private void keepConnecting() {
		MyApplication myApplication = (MyApplication)getApplication();
		RequestObject requestobj = new RequestObject();
		requestobj.method = "keepConnecting";
		String requestUrl = RequestBuilder.build(requestobj);
		EasyDataClass dc = new EasyDataClass();
		try {
			IjConnect.doRequest(requestUrl, dc);
			if (dc.code == null) {				
			} else if (!dc.code.equals(CommonData.RESULT_SUCCESS)) {
				if(myApplication != null) {
					myApplication.stopAutoLogin();
				}
			}
		} catch (Exception e) {		
			e.printStackTrace();
		}
		if(autoLogin) {
			if(myApplication != null) {
				myApplication.continueAutoLogin(DELAY);
			}
		}
	}
	
}
